package com.springboot.cloud.common.core.jiguang;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jmessage.api.JMessageClient;
import cn.jmessage.api.common.model.RegisterInfo;
import cn.jmessage.api.common.model.UserPayload;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Configuration
@Component
@Slf4j
public class JiGuangUtil {

    @Value("${jiguang.appkey}")
    private String appkey;

    @Value("${jiguang.masterSecret}")
    private String masterSecret;

    public void registerUser(RegisterInfo registerInfo ) {
        JMessageClient client = new JMessageClient(appkey, masterSecret);
        try {
            List<RegisterInfo> users = new ArrayList<RegisterInfo>();

            users.add(registerInfo);
            RegisterInfo[] regUsers = new RegisterInfo[users.size()];
            String res = client.registerUsers(users.toArray(regUsers));
            log.info(res);
        } catch (APIConnectionException e) {
            log.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            log.error("Error response from JPush server. Should review and fix it. ", e);
            log.info("HTTP Status: " + e.getStatus());
            log.info("Error Message: " + e.getMessage());
        }
    }
    public  void updatePassword() {
        JMessageClient client = new JMessageClient(appkey, masterSecret);

        try {
            client.updateUserPassword("test_user", "test_new_pass");
        } catch (APIConnectionException e) {
            log.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            log.error("Error response from JPush server. Should review and fix it. ", e);
            log.info("HTTP Status: " + e.getStatus());
            log.info("Error Message: " + e.getMessage());
        }
    }

    public  void updateUserInfo(String username, String nickname, String birthday, String signature, int gender, String region, String address, String avatar) {
        JMessageClient client = new JMessageClient(appkey, masterSecret);

        try {
            UserPayload.Builder builder= UserPayload.newBuilder();

            builder.setNickname(nickname);
            builder.setBirthday(birthday);
            builder.setGender(gender);
            builder.setRegion(region);
            builder.setSignature(signature);
            builder.addExtra("avatarUrl",avatar);
            UserPayload userPayload= builder.build();

            client.updateUserInfo(username,userPayload);
            //client.updateUserInfo( username,  nickname,  birthday,  signature,  gender,  region,  address,  avatar);
        } catch (APIConnectionException e) {
            log.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            log.error("Error response from JPush server. Should review and fix it. ", e);
            log.info("HTTP Status: " + e.getStatus());
            log.info("Error Message: " + e.getMessage());
        }
    }


    public static void main(String[] args) throws Exception {

    }
}
